package org.example.memora.service.serviceImpl;

import org.example.memora.common.result.Result;
import org.example.memora.entity.CommunityUser;
import org.example.memora.mapper.CommunityUserMapper;
import org.example.memora.service.CommunityUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CommunityUserServiceImpl implements CommunityUserService {

    @Autowired
    private CommunityUserMapper communityUserMapper;

    @Override
    public CommunityUser getById(Long id) {
        return communityUserMapper.selectById(id);
    }

    @Override
    public List<CommunityUser> getByCommunityId(Long communityId) {
        return communityUserMapper.selectByCommunityId(communityId);
    }

    @Override
    public Result<?> joinCommunity(CommunityUser user) {
        int rows = communityUserMapper.insert(user);
        if(rows > 0){
            return Result.success();
        }
        return Result.error("加入失败");
    }

    @Override
    public boolean leaveCommunity(Long communityId, Long userId) {
        int rows = communityUserMapper.deleteByCommunityIdAndUserId(communityId, userId);
        return rows > 0;
    }

    @Override
    public boolean updateMemberRole(CommunityUser user) {
        int rows = communityUserMapper.updateById(user);
        return rows > 0;
    }
}

